package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.utils.AreaUtil;
import com.shaoshaohuo.app.utils.city.Cityinfo;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.Address;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.utils.CityinfoUtil;
import com.shaoshaohuo.app.utils.StringHelper;
import com.shaoshaohuo.app.view.citymenu.CascadingMenuPopWindow;
import com.shaoshaohuo.app.view.citymenu.interfaces.CascadingMenuViewOnSelectListener;
import com.shaoshaohuo.app.view.citymenu.entity.Area;

import java.io.Serializable;

/**
 * 新增地址 编辑地址 页面 编辑 状态 传入 address isedit = true 新增状态
 * 
 * @author cao
 *
 */
public class AddressEditActivity extends BaseActivity implements OnClickListener {

	private EditText et_name;
	private EditText et_phone;
	private LinearLayout ll_address;
	private TextView tv_address;
	private EditText et_detail_address;
	private TopbarView topbar;

	private Address address; // 如果是 编辑 接收 传递过来的 地址
	private boolean isEdit;// 用于 标识 是否是编辑
	private Button button_next;

	private Cityinfo marketCity; // 选中的市
	private boolean setDefault;
	

	private CascadingMenuPopWindow cascadingMenuPopWindowMarket;
	
	private boolean isCommit = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address_edit);
		Intent intent = getIntent();
		Serializable serializable = intent.getSerializableExtra(ExtraName.address);
		if (serializable != null) {
			isEdit = true;
			address = (Address) serializable;
		}
		setDefault = intent.getBooleanExtra("setDefault", false);
		initView();
		initData();
	}

	private void initView() {
		topbar = (TopbarView) findViewById(R.id.topbar);
		et_name = (EditText) findViewById(R.id.et_name);
		et_phone = (EditText) findViewById(R.id.et_phone);
		ll_address = (LinearLayout) findViewById(R.id.ll_address);
		tv_address = (TextView) findViewById(R.id.tv_address);
		et_detail_address = (EditText) findViewById(R.id.et_detail_address);
		button_next = (Button) findViewById(R.id.button_next);

	}

	private void initData() {
		if (isEdit) {
			topbar.setCenterText("编辑地址");
			et_name.setText(address.getReceiver_name());
			et_phone.setText(address.getReceiver_phone());
			tv_address.setText(AreaUtil.getAreaFullname(address.getCityid()));
			et_detail_address.setText(address.getAddress());
			
			marketCity = new Cityinfo();

			marketCity.setId(address.getId());
			marketCity.setCityId(address.getCityid());
		} else {
			topbar.setCenterText("新增地址");
		}

		topbar.setLeftView(true, true);
		topbar.setLeftClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		ll_address.setOnClickListener(this);
		button_next.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ll_address:
			selectCity();
			break;
		case R.id.button_next:
			commit();
			break;
		}
	}

	/**
	 * 选择 城市
	 */
	private void selectCity() {
		if (cascadingMenuPopWindowMarket == null) {
			// 向三级menu添加地区数据
			cascadingMenuPopWindowMarket = new CascadingMenuPopWindow(this, false);
			cascadingMenuPopWindowMarket.setMenuViewOnSelectListener(new CascadingMenuViewOnSelectListener() {

				@Override
				public void getValue(Area city, String fullname) {
					marketCity = CityinfoUtil.convert(city);

					tv_address.setText(fullname);
				}

			});
			cascadingMenuPopWindowMarket.showAsDropDown(tv_address, 5, 5);
		} else if (cascadingMenuPopWindowMarket != null && cascadingMenuPopWindowMarket.isShowing()) {
			cascadingMenuPopWindowMarket.dismiss();
		} else {
			cascadingMenuPopWindowMarket.showAsDropDown(tv_address, 5, 5);
		}
	}

	private void commit() {
		if(isCommit){
			return;
		}
		isCommit=true;
		String name = et_name.getText().toString().trim();
		String phone = et_phone.getText().toString().trim();
		String detailAddress = et_detail_address.getText().toString().trim();
		if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(detailAddress)
				|| marketCity == null) {
			showToast("请填写完整！");
			isCommit = false;
			return;
		}
		if(!StringHelper.isPhoneNumber(phone)){
			showToast("请输入合格的联系人电话");
			isCommit = false;
			return;
		}
		Address address = new Address();
		address.setAddress(detailAddress);
		address.setId(marketCity.getId());
		address.setReceiver_name(name);
		address.setReceiver_phone(phone);
		address.setCityid(marketCity.getCityId());
		if(setDefault)address.setIs_default("1");
		startLoadingDialog();
		RequestService.getInstance().addAddress(this, address, BaseEntity.class, new RequestListener() {
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if (resultData.isOk()) {
					setResult(RESULT_OK);
					finish();
				}
				isCommit=false;
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(msg);
				isCommit=false;
			}
		});
	}

	/**
	 * 如果 现在状态是 编辑状态 当用户按返回键时 需要保存以前的 address
	 */
	@Override
	public void onBackPressed() {
		if (isEdit) {
			startLoadingDialog();
			RequestService.getInstance().addAddress(this, address, BaseEntity.class, new RequestListener() {
				@Override
				public void onSuccess(int requestCode, BaseEntity resultData) {
					dismissLoadingDialog();
					if (resultData.isOk()) {
					}
				}

				@Override
				public void onFailed(int requestCode, Exception error, String msg) {
					dismissLoadingDialog();
					showToast(msg);
				}
			});
		}
		super.onBackPressed();
	}

}
